#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{   
    ui->setupUi(this);
    ui_init();
    ui->lineEdit_save_patch->setText("D:/work/share");
    image_c3100_init();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::get_image_msg(image_message_t data)
{
    image_message_t my_image_msg;

    my_image_msg = data;

    if((my_image_msg.width == 0) || (my_image_msg.hight == 0))
    {
        QMessageBox::warning(this , "提示", "参数异常保存失败!");
        return;
    }

    QString filename = ui->lineEdit_tab1_filePatch->text();
    image_change image;
    QByteArray arry = image.get_image_arry(filename,my_image_msg);




    QString path = QFileDialog::getSaveFileName(this,
        tr("Open File"),
        ".",
        tr("Bin Files(*.bin)"));


    QFile f(path);      //定义一个文件
    f.open(QIODevice::WriteOnly);   //打开文件
    QDataStream qs(&f);             //定义一个数据流
    for(int i=0;i<arry.length();i++)
    {
        qs << (uint8_t)arry[i];      //把数组中的数据写到数据流，即写入文件中
    }
    f.close();

//    if(!path.isEmpty()) {
//        QFile file(path);
//        if (!file.open(QIODevice::WriteOnly)) {
//            QMessageBox::warning(this, tr("Write File"),
//                tr("Cannot open file:\n%1").arg(path));
//            return;
//        }
//        file.write(arry);
//        file.close();
//    }

}


/*
 * 函数名：
 * 功能：将图片素材通过压缩后转换成png二进制格式,数据从左至右竖着存
 * 输入：
 * 输出：
 * 作者：lzq
 * 时间：2024.3.15
*/
void MainWindow::on_pushButton_tab1_choose_clicked()
{
    QString filename = QFileDialog::getOpenFileName(this,tr("Open Image"),"C:/Users",tr("(*.png)\n(*.bmp)\n(*.jpg)"));   //打开图片文件，选择图片
    ui->lineEdit_tab1_filePatch->setText(filename);
}

void MainWindow::on_pushButton_tab1_save_clicked()
{
    image_set *image_buff = new image_set;
    image_buff->show();

    connect(image_buff,&image_set::get_image_msg,this,&MainWindow::get_image_msg);
}

void MainWindow::ui_init()
{
    ui->tabWidget->setTabText(0,"图像转换");
    ui->tabWidget->setTabText(1,"GBK编码转换");
    ui->tabWidget->setTabText(2,"渐变色图像生成");
    ui->tabWidget->setTabText(3,"300_config");

    ui->pushButton_save_patch->setText("设置存储路径");
}

void MainWindow::image_c3100_init()
{

}

void MainWindow::on_buttonBox_clicked(QAbstractButton *button)
{

}

void MainWindow::on_pushButton_text2GBK_clicked()
{
    QString china_str = ui->textEdit_text->toPlainText();

    QTextCodec::setCodecForLocale(QTextCodec::codecForName("utf-8"));
    QTextCodec *gbk = QTextCodec::codecForName("GB18030");
    QTextCodec *utf8 = QTextCodec::codecForName("UTF-8");

    QString strUnicode;
    QByteArray str_arry;
    strUnicode = utf8->toUnicode(china_str.toLocal8Bit().data());
    str_arry = gbk->fromUnicode(strUnicode);

    QString str;
    foreach (quint8 ch, str_arry) {
       str.append(QString("%1 ").arg(ch,2,16,QLatin1Char( '0' )));
    }

    ui->textEdit_GBK->setText(str);
}

void MainWindow::on_pushButton_GBK2text_clicked()
{
    QString str = ui->textEdit_GBK->toPlainText();

    QByteArray arry = QByteArray::fromHex(str.replace(" ","").toUtf8());
    str = gbk2utf8(arry);

    ui->textEdit_text->setText(str);
}

QString MainWindow::gbk2utf8(QByteArray array)
{
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("utf-8"));
    QTextCodec *gbk = QTextCodec::codecForName("GB18030");
    QTextCodec *utf8 = QTextCodec::codecForName("UTF-8");

    QString str = utf8->fromUnicode(gbk->toUnicode(array));			  // gbk  convert utf8
    return str;
}

void MainWindow::on_pushButton_red_clicked()
{
    image_c3100 my_image;
    picture_msg_t picture_message;
    picture_message.red = 0;
    picture_message.blue = 0;
    picture_message.green = 0;
    picture_message.width = 256;
    picture_message.hight = 64;
    if(ui->lineEdit_save_patch->text().isNull())
        picture_message.patch = "C:";
    else
        picture_message.patch = ui->lineEdit_save_patch->text();
    picture_message.type = 1;
    picture_message.file_name = "red";
    my_image.creat_picture(&picture_message);

    QMessageBox::warning(this , QString("%1.png").arg(picture_message.file_name), "图像生成成功!");
}

void MainWindow::on_pushButton_green_clicked()
{
    image_c3100 my_image;
    picture_msg_t picture_message;
    picture_message.red = 0;
    picture_message.blue = 0;
    picture_message.green = 0;
    picture_message.width = 256;
    picture_message.hight = 64;
    if(ui->lineEdit_save_patch->text().isNull())
        picture_message.patch = "C:";
    else
        picture_message.patch = ui->lineEdit_save_patch->text();
    picture_message.type = 2;
    picture_message.file_name = "green";
    my_image.creat_picture(&picture_message);

    QMessageBox::warning(this , QString("%1.png").arg(picture_message.file_name), "图像生成成功!");
}

void MainWindow::on_pushButton_blue_clicked()
{
    image_c3100 my_image;
    picture_msg_t picture_message;
    picture_message.red = 0;
    picture_message.blue = 0;
    picture_message.green = 0;
    picture_message.width = 256;
    picture_message.hight = 64;
    if(ui->lineEdit_save_patch->text().isNull())
        picture_message.patch = "C:";
    else
        picture_message.patch = ui->lineEdit_save_patch->text();
    picture_message.type = 3;
    picture_message.file_name = "blue";
    my_image.creat_picture(&picture_message);

    QMessageBox::warning(this , QString("%1.png").arg(picture_message.file_name), "图像生成成功!");
}

void MainWindow::on_pushButton_white_clicked()
{
    image_c3100 my_image;
    picture_msg_t picture_message;
    picture_message.red = 0;
    picture_message.blue = 0;
    picture_message.green = 0;
    picture_message.width = 256;
    picture_message.hight = 64;
    if(ui->lineEdit_save_patch->text().isNull())
        picture_message.patch = "C:";
    else
        picture_message.patch = ui->lineEdit_save_patch->text();
    picture_message.type = 0;
    picture_message.file_name = "white";
    my_image.creat_picture(&picture_message);

    QMessageBox::warning(this , QString("%1.png").arg(picture_message.file_name), "图像生成成功!");
}

void MainWindow::on_pushButton_save_patch_clicked()
{
    QFileDialog *fileDialog = new QFileDialog(this);
    fileDialog->setFileMode(QFileDialog::Directory);
    fileDialog->exec();
    auto selectDir = fileDialog->selectedFiles();
    if (selectDir.size()>0)
    {
        ui->lineEdit_save_patch->setText(selectDir.at(0));
    }
    else
    {
        QMessageBox::warning(this , "提示", "保存路径错误！!");
    }

}

void MainWindow::on_pushButton_white_plus_clicked()
{
    image_c3100 my_image;
    picture_msg_t picture_message;
    picture_message.red = 0;
    picture_message.blue = 0;
    picture_message.green = 0;
    picture_message.width = 256;
    picture_message.hight = 64;
    if(ui->lineEdit_save_patch->text().isNull())
        picture_message.patch = "C:";
    else
        picture_message.patch = ui->lineEdit_save_patch->text();
    picture_message.type = 4;
    picture_message.file_name = "white_plus";
    my_image.creat_picture(&picture_message);

    QMessageBox::warning(this , QString("%1.png").arg(picture_message.file_name), "图像生成成功!");
}

void MainWindow::on_pushButton_creat_bin_clicked()
{
    e300_config e300;
//    QByteArray pack = e300.xin_hong_creat_program_side(); //侧牌样式
//    QByteArray pack = e300.xin_hong_creat_program_wei(); //尾牌配置
    QByteArray pack = e300.xin_hong_creat_program_qian();//头牌配置
//    QByteArray pack = e300.xin_hong_creat_program_test();//产测配置
    QString str = QString("data length:%1 \n").arg(pack.length());
    foreach (quint8 ch, pack) {
       str.append(QString("%1 ").arg(ch,2,16,QLatin1Char( '0' )));
    }
    ui->textEdit_e300_show->setText(str);



    QString path = QFileDialog::getSaveFileName(this,
        tr("Open File"),
        "D:/work/share",
        tr("Bin Files(*.bin)"));


    QFile f(path);      //定义一个文件
    f.open(QIODevice::WriteOnly);   //打开文件
    QDataStream qs(&f);             //定义一个数据流
    for(int i=0;i<pack.length();i++)
    {
        qs << (uint8_t)pack[i];      //把数组中的数据写到数据流，即写入文件中
    }
    f.close();
}

void MainWindow::on_pushButton_creat_illegal_words_clicked()
{
    e300_config e300;

    QString file_patch = QFileDialog::getOpenFileName(this,
        tr("Open File"),
        "./",
        tr("Bin Files(*.json)"));
    QByteArray pack = e300.get_illegal_words(file_patch);

    if(pack.length() == 0)
    {
        DEBUG_ERROR("creat_illegal_words faile");
        return;
    }

    QString str = QString("data length:%1 \n").arg(pack.length());
    foreach (quint8 ch, pack) {
       str.append(QString("%1 ").arg(ch,2,16,QLatin1Char( '0' )));
    }
    ui->textEdit_e300_show->setText(str);


    QString path = QFileDialog::getSaveFileName(this,
        tr("Open File"),
        "D:/work/share",
        tr("Bin Files(*.bin)"));
    QFile f(path);      //定义一个文件
    f.open(QIODevice::WriteOnly);   //打开文件
    QDataStream qs(&f);             //定义一个数据流
    for(int i=0;i<pack.length();i++)
    {
        qs << (uint8_t)pack[i];      //把数组中的数据写到数据流，即写入文件中
    }
    f.close();
}

void MainWindow::on_pushButton48x16_clicked()
{
    image_c3100 my_image;
    picture_msg_t picture_message;
    picture_message.red = 255;
    picture_message.blue = 0;
    picture_message.green = 0;
    picture_message.width = 48;
    picture_message.hight = 16;
    if(ui->lineEdit_save_patch->text().isNull())
        picture_message.patch = "D:";
    else
        picture_message.patch = ui->lineEdit_save_patch->text();
    picture_message.type = 5;
    picture_message.file_name = "white_plus";
    my_image.creat_picture(&picture_message);

    QMessageBox::warning(this , QString("%1.png").arg(picture_message.file_name), "图像生成成功!");
}
